<?php
namespace Admin\Model;
use Think\Model;
class ResourceModel extends Model {

   public function info(){
   	   //接受页数
   	    $page =I('get.page');
   	    if(!$page){
            $page = 1;
   	    }
   	    $num = 8;
   	    $count = ($page-1)*$num;
   	    $where = array('sid' => array('eq','0'),'userid' =>session('userid'));
         //分页
        $resCount = $this->where($where)->count();
         //总页数
        if($resCount==1){
         	$resPage =1;
        }else{
         	$resPage = ceil($resCount/$num);
        }
        //取出资源  
        $resourceInfo = $this->where($where)->limit($count , $num)->select();
        //分配数据
        return array(
			   'data' => $resourceInfo,
			   'page' => $page,
			   'count' => $resCount,
			   'resPage' =>$resPage,
		   );
   }

   //资源使用情况
   public function zysyqk($sid){
      //接受页数
   	    $page =I('get.page');
   	    if(!$page){
            $page = 1;
   	    }
   	    //自定义取的条数
   	    $num = 8;
   	    $count = ($page-1)*$num;
        //默认条件
   	    $where = array('sid' => array('neq','0'),'userid' =>session('userid'));
   	    //查询条件
   	    if($sid){
   	    	$where = array('sid' => $sid,'userid' =>session('userid'));
   	    }
         //分页
         $resCount = $this->where($where)->count();
         //一共几页
        if($resCount==1){
         	  $resPage =1;
         }else{
         	  $resPage = ceil($resCount/$num);
         }
         //取出数据
         $resourceInfo = $this->where($where)->limit($count , $num)->select();
        //分配资源
         return array(
			    'data' => $resourceInfo,
			    'count' => $resCount,
			    'resPage' =>$resPage,
			    'page' =>$page,
		    );
   }


   //资源使用情况查看
   // public function resInfo(){
   //       //接受查询条件
   //      if(session('roleid')==2){
   //        echo session('gid'),session('tid'),session('did'),session('sid');
   //        die;
   //      }
   //        $gid = I('get.groupId');  //分组
   //        $did = session('did');    //区id
   //        $sid = session('sid');
   //        $tid = session('tid');
   //        $startTime = strtotime(I('get.startTime'));
   //        $endTime = strtotime(I('get.endTime'))+86400;
   //        //时间是否相等，相等设成当前时间
   //        if($startTime == $endTime){
   //          $endTime=time();
   //        }
   //        //查处该部门下所有的成员
   //        $userModel = D('user');
   //       if($gid!="" && $gid!="-1"){
   //            $userid = $userModel->where(array('did' => $did,'gid' => $gid,'roleid' => array('neq',1)))->field('userid')->select();
   //        }else{
   //           $userid = $userModel->where(array('did' => $did,'roleid'=>array('neq',1)))->field('userid')->select();
   //        }
   //        $arr = array();
   //            foreach ($userid as $k => $r){
   //              foreach ($r as $k => $v) {
   //                $arr[]=$v;
   //              }
   //            }
   //       $exp = implode(',',$arr);
   //       //查询条件
   //       //按分组
   //      if($gid!='-1' && $startTime==""){
   //         $where = array('userid' =>array('in',$exp),'onjob'=>array('eq', 0));
   //      }else{
   //        //按时间或时间和分组
   //        $where = array('userid' =>array('in',$exp),'onjob'=>array('eq', 0),'updatetime'=>array('gt',$startTime),'updatetime'=>array('lt',$endTime ));
   //      }
   //      $resourceInfo = $this->where($where)->field('sid')->select();
   //      static $res = array();
   //      // dump($resourceInfo);die;
   //      foreach ($resourceInfo as $k => $v) {
   //      	 if($v['sid']==0){
   //            $res['0'][]=$v;
   //         }elseif ($v['sid']==1) {
   //            $res['1'][]=$v;
   //         }elseif ($v['sid']==2) {
   //           $res['2'][]=$v;
   //         }elseif ($v['sid']==4) {
   //           $res['4'][]=$v;
   //         }elseif ($v['sid']==5) {
   //           $res['5'][]=$v;
   //         }else{
   //            $res['3'][]=$v;
   //         }
   //      }
   //      $resCount[0]=count($res['0']);
   //      $resCount[1]=count($res['1']);
   //      $resCount[2]=count($res['2']);
   //      $resCount[3]=count($res['3']);
   //      $resCount[4]=count($res['4']);
   //      $resCount[5]=count($res['5']);

   //      return $resCount;

   // }

   //资源使用情况查询
   public function resInfo()
   {

    if(session('roleid')==0)
      {
        $where['did']  = I('get.groupId');
        $where['sid']  = I('get.buid');
        $where['gid']  = I('get.zuid');
        $where['tid']  = I('get.duiid');
      
        if($where['did'] == -1){
          unset($where['did']);
        }
        if($where['sid'] == -1){
          unset($where['sid']);
        }
        if($where['gid'] == -1){
          unset($where['gid']);
        }
        if($where['tid'] == -1){
          unset($where['tid']);
        }

        //查看该部门下面的所有成员
        $userModel = D('user');
          $userid = $userModel->where($where)->field('userid')->select();
            $arr = array();
              foreach ($userid as $k => $r){
                foreach ($r as $k => $v) {
                  $arr[]=$v;
                }
            }
         $exp = implode(',',$arr);

         //按条件查询
          $where = array('userid' =>array('in',$exp),'onjob'=>array('eq', 0));
         $resourceInfo = $this->where($where)->field('sid')->select();
        static $res = array();
        // dump($resourceInfo);die;
        foreach ($resourceInfo as $k => $v) {
          if($v['sid']==0){
              $res['0'][]=$v;
           }elseif ($v['sid']==1) {
              $res['1'][]=$v;
           }elseif ($v['sid']==2) {
             $res['2'][]=$v;
           }elseif ($v['sid']==4) {
             $res['4'][]=$v;
           }elseif ($v['sid']==5) {
             $res['5'][]=$v;
           }else{
              $res['3'][]=$v;
           }
        }
        $resCount[0]=count($res['0']);
        $resCount[1]=count($res['1']);
        $resCount[2]=count($res['2']);
        $resCount[3]=count($res['3']);
        $resCount[4]=count($res['4']);
        $resCount[5]=count($res['5']);

        return $resCount;
      }


    if(session('roleid')==1)
      {
        if(empty(I('get.groupId'))){
          $where['sid'] = session('sid');
        }else{
          $where['sid'] = I('get.groupId');
        }
        if(!empty(I('get.zuid'))){
          $where['gid']  = I('get.zuid');
        }
        if(!empty(I('get.duiid'))){
          $where['tid']  = I('get.duiid');
        }
        if($where['tid']==-1){
          unset($where['tid']);
        }
        if($where['gid']==-1){
          unset($where['gid']);
        }
        $roleid = session('roleid');
        $where['did'] = session('did');
        //查看该部门下面的所有成员
        $userModel = D('user');
        if($sid !== "" && $sid !== -1)
        {
          $userid = $userModel->where($where)->field('userid')->select();
        }else{
          $userid = $userModel->where($where)->field('userid')->select();
        }
            $arr = array();
              foreach ($userid as $k => $r){
                foreach ($r as $k => $v) {
                  $arr[]=$v;
                }
            }
         $exp = implode(',',$arr);

         // dump($exp);  //userid
         //按条件查询
          $where = array('userid' =>array('in',$exp),'onjob'=>array('eq', 0));
         $resourceInfo = $this->where($where)->field('sid')->select();
        static $res = array();
        // dump($resourceInfo);die;
        foreach ($resourceInfo as $k => $v) {
          if($v['sid']==0){
              $res['0'][]=$v;
           }elseif ($v['sid']==1) {
              $res['1'][]=$v;
           }elseif ($v['sid']==2) {
             $res['2'][]=$v;
           }elseif ($v['sid']==4) {
             $res['4'][]=$v;
           }elseif ($v['sid']==5) {
             $res['5'][]=$v;
           }else{
              $res['3'][]=$v;
           }
        }
        $resCount[0]=count($res['0']);
        $resCount[1]=count($res['1']);
        $resCount[2]=count($res['2']);
        $resCount[3]=count($res['3']);
        $resCount[4]=count($res['4']);
        $resCount[5]=count($res['5']);

        return $resCount;
      }

      if(session('roleid')==3)
      {
        if(empty(I('get.groupId'))){
          $tid = session('tid');
        }else{
          $tid = I('get.groupId');
        }
        $sid = session('sid');
        $did = session('did');
        $gid = session('gid');
        $roleid = session('roleid');
        //查看该部门下面的所有成员
        $userModel = D('user');
        if($tid !== "" && $tid !== -1)
        {
          $userid = $userModel->where(array('gid'=>$gid,'did'=>$did,'sid'=>$sid,'tid'=>$tid))->field('userid')->select();
        }else{
          $userid = $userModel->where(array('did'=>$did,'sid'=>$sid,'gid'=>$gid))->field('userid')->select();
        }
            $arr = array();
              foreach ($userid as $k => $r){
                foreach ($r as $k => $v) {
                  $arr[]=$v;
                }
            }
         $exp = implode(',',$arr);

         // dump($exp);  //userid

         //按条件查询
          $where = array('userid' =>array('in',$exp),'onjob'=>array('eq', 0));
         $resourceInfo = $this->where($where)->field('sid')->select();

        static $res = array();
        // dump($resourceInfo);die;
        foreach ($resourceInfo as $k => $v){
          if($v['sid']==0){
              $res['0'][]=$v;
           }elseif ($v['sid']==1) {
              $res['1'][]=$v;
           }elseif ($v['sid']==2) {
             $res['2'][]=$v;
           }elseif ($v['sid']==4) {
             $res['4'][]=$v;
           }elseif ($v['sid']==5) {
             $res['5'][]=$v;
           }else{
              $res['3'][]=$v;
           }
        }
        $resCount[0]=count($res['0']);
        $resCount[1]=count($res['1']);
        $resCount[2]=count($res['2']);
        $resCount[3]=count($res['3']);
        $resCount[4]=count($res['4']);
        $resCount[5]=count($res['5']);

        return $resCount;
      }
   

    if(session('roleid')==2)
      {

        if(empty(I('get.groupId'))){
          $where['gid'] = session('gid');
        }else{
          $where['gid'] = I('get.groupId');
        }

        if(!empty(I('get.duiid'))){
          $where['tid'] = I('get.duiid');
        }
        if($where['tid']==-1){
          unset($where['tid']);
        }
        $where['sid'] = session('sid');
        $where['did'] = session('did');
        //查看该部门下面的所有成员
        $userModel = D('user');
        if($gid !== "" && $gid !== -1)
        {
          $userid = $userModel->where($where)->field('userid')->select();
        }else{
          $userid = $userModel->where($where)->field('userid')->select();
        }
            $arr = array();
              foreach ($userid as $k => $r){
                foreach ($r as $k => $v) {
                  $arr[]=$v;
                }
            }
         $exp = implode(',',$arr);

         // dump($exp);  //userid
         //按条件查询
          $where = array('userid' =>array('in',$exp),'onjob'=>array('eq', 0));
         $resourceInfo = $this->where($where)->field('sid')->select();
        static $res = array();
        // dump($resourceInfo);die;
        foreach ($resourceInfo as $k => $v) {
          if($v['sid']==0){
              $res['0'][]=$v;
           }elseif ($v['sid']==1) {
              $res['1'][]=$v;
           }elseif ($v['sid']==2) {
             $res['2'][]=$v;
           }elseif ($v['sid']==4) {
             $res['4'][]=$v;
           }elseif ($v['sid']==5) {
             $res['5'][]=$v;
           }else{
              $res['3'][]=$v;
           }
        }
        $resCount[0]=count($res['0']);
        $resCount[1]=count($res['1']);
        $resCount[2]=count($res['2']);
        $resCount[3]=count($res['3']);
        $resCount[4]=count($res['4']);
        $resCount[5]=count($res['5']);

        return $resCount;
      }
      if(session('roleid')==4)
      {
        $did = session('did');
        $gid = session('gid');
        $sid = session('sid');
        $tid = session('tid');
        $userModel = D('user');
        $userid = $userModel->where(array('gid'=>$gid,'did'=>$did,'sid'=>$sid,'tid'=>$tid))->field('userid')->select();
        $arr = array();
              foreach ($userid as $k => $r){
                foreach ($r as $k => $v) {
                  $arr[]=$v;
                }
            }
         $exp = implode(',',$arr);
         $where = array('userid' =>array('in',$exp),'onjob'=>array('eq', 0));
         $resourceInfo = $this->where($where)->field('sid')->select();
        static $res = array();
        // dump($resourceInfo);die;
        foreach ($resourceInfo as $k => $v) {
          if($v['sid']==0){
              $res['0'][]=$v;
           }elseif ($v['sid']==1) {
              $res['1'][]=$v;
           }elseif ($v['sid']==2) {
             $res['2'][]=$v;
           }elseif ($v['sid']==4) {
             $res['4'][]=$v;
           }elseif ($v['sid']==5) {
             $res['5'][]=$v;
           }else{
              $res['3'][]=$v;
           }
        }
        $resCount[0]=count($res['0']);
        $resCount[1]=count($res['1']);
        $resCount[2]=count($res['2']);
        $resCount[3]=count($res['3']);
        $resCount[4]=count($res['4']);
        $resCount[5]=count($res['5']);

        return $resCount;
      }


   
   }


    //主任组员管理
   public function zuyuaninfo($userid,$sid){

        $page =I('get.page');
        if(!$page){
            $page = 1;
        }
        $num = 13;
        $count = ($page-1)*$num;
        $where = array('userid' =>$userid ,);
        if($sid!=-1){
            if($sid==3){
               $where = array('userid' =>$userid ,'sid' =>array('in','6,7,8,9', ));
            }else{
               $where = array('userid' =>$userid ,'sid'=>$sid );
            }
        }
         //分页
         $resCount = $this->where($where)->count();
         //总页数
         if($resCount==1){
            $resPage =1;
         }else{
            $resPage = ceil($resCount/$num);
         }
         //取出数据
        $resourceInfo = $this->where($where)->limit($count , $num)->select();
         return array(
           'data' => $resourceInfo,
           'page' => $page,
           'count' => $resCount,
           'resPage' =>$resPage,
        );
   }
   //离职员工资源
    public function liZhiInfo(){
         $page =I('get.page');
        if(!$page){
            $page = 1;
        }
        $num = 6;
        $count = ($page-1)*$num;
        //默认条件
        $where = array('onjob' =>1 , );
         //分页
         $resCount = $this->where($where)->count();
         //总页数
         if($resCount==1){
          $resPage =1;
         }else{
          $resPage = ceil($resCount/$num);
         }
         //取出数据
        $resourceInfo = $this->where($where)->limit($count , $num)->select();
        //取出离职总资源
        $resource = $this->where(array('onjob' =>1 , ))->select();
        
        static $res = array();
        static $resCount = array();
        foreach ($resource as $k => $v) {
           if($v['sid']==0){
              $res['0'][]=$v;
           }elseif ($v['sid']==1) {
              $res['1'][]=$v;
           }elseif ($v['sid']==2) {
             $res['2'][]=$v;
           }elseif ($v['sid']==4) {
             $res['4'][]=$v;
           }elseif ($v['sid']==5) {
             $res['5'][]=$v;
           }else{
              $res['3'][]=$v;
           }
        }
        $resCount[0]=count($res['0']);
        $resCount[1]=count($res['1']);
        $resCount[2]=count($res['2']);
        $resCount[3]=count($res['3']);
        $resCount[4]=count($res['4']);
        $resCount[5]=count($res['5']);

        return array(
          'zcount' =>count($resource),
          'resCount' => $resCount, 
          'data' => $resourceInfo,
          'page' => $page,
          'count' => $resCount,
          'resPage' =>$resPage,
        );
        
   }
}